//
//  CELabelTextField.m
//  CenturyEast
//
//  Created by armada on 2017/9/12.
//  Copyright © 2017年 Leon Guo. All rights reserved.
//

#import "CEAccessoryTextField.h"

@interface CEAccessoryTextField()



@end

@implementation CEAccessoryTextField

#define TF_TITLE_FONT [UIFont systemFontOfSize:16]
#define TF_TITLE_COLOR UIColorFromRGBHex(0x666666)
#define TF_HEIGHT 50.0f

#pragma mark - Constructor

- (instancetype)init {
    
    self = [super init];

    if (self) {
        
//        self.type = CEAccessoryTextFieldTypeNone;
//        self.backgroundColor = [UIColor whiteColor];
//        [self addSubview:self.underlineView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.type = CEAccessoryTextFieldTypeNone;
        self.backgroundColor= [UIColor whiteColor];
        [self addSubview:self.underlineView];
    }
    return self;
}

#pragma mark - Setter

- (void)setType:(CEAccessoryTextFieldType)type {
    
    if (type == _type) {
        return;
    }
    
    self.leftViewMode = UITextFieldViewModeAlways;
    
    switch (type) {
            
        case CEAccessoryTextFieldTypeImageView:
            
            self.leftView = self.iconImageView;
            break;
            
        case CEAccessoryTextFieldTypeLabel:
            
            self.leftView = self.titleLabel;
            break;
    
        default:
            self.leftView = nil;
            break;
    }
}

- (void)setClearButtonMode:(UITextFieldViewMode)clearButtonMode {
    [super setClearButtonMode:clearButtonMode];
}

#pragma mark - Lazy Loading 

- (UILabel *)titleLabel {
    
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 70, TF_HEIGHT)];
        _titleLabel.font = TF_TITLE_FONT;
        //_titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = TF_TITLE_COLOR;
    }
    return _titleLabel;
}

- (UIImageView *)iconImageView {
    
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _iconImageView;
}

- (UIView *)underlineView {
    
    if (!_underlineView) {
        _underlineView = [[UIView alloc] initWithFrame:CGRectMake(20, 50, kDeviceWidth - 40  , .5)];
        _underlineView.backgroundColor = TF_TITLE_COLOR;
        [self addSubview:_underlineView];
    }
    return _underlineView;
    
}

#pragma mark - Drawing and positioning overrides

- (CGRect)textRectForBounds:(CGRect)bounds {
    
    CGRect originRect = [super textRectForBounds:bounds];
    
    return CGRectMake(originRect.origin.x + 20, originRect.origin.y+2, originRect.size.width, originRect.size.height);
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    
    CGRect originRect = [super editingRectForBounds:bounds];
    
    return CGRectMake(originRect.origin.x + 20, originRect.origin.y+2, originRect.size.width, originRect.size.height);
}

- (CGRect)leftViewRectForBounds:(CGRect)bounds {
    
    CGRect originRect = [super leftViewRectForBounds:bounds];
    
    return CGRectMake(originRect.origin.x + 20, originRect.origin.y, originRect.size.width, originRect.size.height);
}

- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
    
    return [super clearButtonRectForBounds:bounds];
}


@end
